package com.yanqu.road.server.cmd.activity.douluodalu;


import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuPatronsTimesData;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuServerUserData;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_ZHUI_BU_OR_TIAO_ZHAN,desc = "追捕/挑战，区别与自动匹配对手")
public class DouLuoDaLuZhuiBuOrTiaoZhanCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInDuiJuePeriod()) != 0) return;

        DouLuoDaLuProto.DouLuoDaLuZhuiBuOrTiaoZhanReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuZhuiBuOrTiaoZhanReqMsg.parseFrom(packet.getBytes());
        int type = reqMsg.getType();

        if(type != 2 && type != 3){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        int patronsId = reqMsg.getPatronsId();
        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
        if (userPatrons == null) {
            ret = GameErrorCode.E_PATRONS_NO_FOUND;
            return;
        }

        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        //门客次数
        DouLuoDaLuModule daLuModule = player.getModule(DouLuoDaLuModule.class);
        DouLuoDaLuServerUserData userData = daLuModule.getUserData();
        String consume;
        synchronized (userData) {
            DouLuoDaLuPatronsTimesData patronsTimesData = daLuModule.getPatronsTimesDataMap().get(patronsId);

            if (patronsTimesData != null) {
                if (type == 3 && patronsTimesData.getFightTimes() >= config.FIGHT_GUEST_OUT) {
                    ret = GameErrorCode.E_DOU_LUO_DA_LU_NO_PATRONS_OUT_TIMES;
                    return;
                }
                if (type == 2 && patronsTimesData.getZhuiBuTimes() >= config.FIGHT_GUEST_ZHUIBU) {
                    ret = GameErrorCode.E_DOU_LUO_DA_LU_NO_PATRONS_ZHUI_BU_TIMES;
                    return;
                }
            }

            if (type == 2) {
                consume = config.ZHUIBU_ITEM_ID + "=" + config.ZHUIBU_ITEM_NUM;
            } else {
                consume = config.TIAOZHAN_ITEM_ID + "=" + config.TIAOZHAN_ITEM_NUM;
            }

            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(consume))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }

            //预扣道具
            player.getModule(PreReduceModule.class).preReduce(consume);

            //预扣次数
            if(type == 2){
                userData.setZhuiBuTimes(1 + userData.getZhuiBuTimes());
            }else if(type == 3){
                userData.setFightTimes(1 + userData.getFightTimes());
            }
            if (patronsTimesData == null) {
                daLuModule.initPatronsTimesData(patronsId, 1, type);
            } else {
                if(type == 2){
                    patronsTimesData.setZhuiBuTimes(1 + patronsTimesData.getZhuiBuTimes());
                }else if(type == 3){
                    patronsTimesData.setFightTimes(1 + patronsTimesData.getFightTimes());
                }
            }
        }

        DouLuoDaLuProto.DouLuoDaLuZhuiBuOrTiaoZhanCrossReqMsg.Builder crossReqMsg = DouLuoDaLuProto.DouLuoDaLuZhuiBuOrTiaoZhanCrossReqMsg.newBuilder();
        crossReqMsg.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReqMsg.setType(type);
        crossReqMsg.setConsume(consume);
        crossReqMsg.setEnemyId(reqMsg.getEnemyId());
        crossReqMsg.setPatronsId(patronsId);
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_ZHUI_BU_OR_TIAO_ZHAN, crossReqMsg, crossReqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_ZHUI_BU_OR_TIAO_ZHAN, DouLuoDaLuProto.DouLuoDaLuZhuiBuOrTiaoZhanRespMsg.newBuilder().setRet(ret));
        }
    }
}
